const app = getApp()
var that;
var util = require('../../utils/util.js');

Page({

  /* 页面的初始数据 */
  data: {
    userinfo: '',
  },

  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    that = this
    that.setData({
      userinfo: app.globalData.loginUserObj,
      userMobileInput: app.globalData.loginUserObj.mobile,
      userNameInput: app.globalData.loginUserObj.name
    })
  },
  // 名称输入框赋值
  userNameInput: function (e) {//方法1
    that.setData({
      'userinfo.name': e.detail.value
    })
  },
  // 电话输入框赋值
  userMobileInput: function (e) {//方法1
    that.setData({
      'userinfo.mobile': e.detail.value
    })
  },
  //提交
  subJD(){
    that = this;
    wx.request({
      url: app.http + 'hysUser/update',
      data: that.data.userinfo ,
      method: 'post',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        if (res.data.code == 200) {
          var pages = getCurrentPages();
          console.log(pages)
          var prevPage = pages[0]; //上一个页面
          if (prevPage.route == "pages/pageMe/pageMe") {
            prevPage.getuserinfoJD();
          } 
          var prevPage1 = pages[1]; //上一个页面
          if (prevPage1.route == "pages/pageMeDetail/pageMeDetail") {
             prevPage1.setData({
               userinfo: app.globalData.loginUserObj  
             });
          } 

          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000,
            success(){
                setTimeout(function(){
                  wx.navigateBack({
                    delta: 1
                  })
                },1000)
            }
          })
         

        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.subJD();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.subJD();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("fail返回", err)
      }
    })
  }
})